package bll

import (
	"auth/event"
	"auth/model"
	"auth/model/entity"
	"auth/store"
	"auth/store/postgres"
	"context"
)

type userJob struct {
	iUserJob store.IUserJob
}

var UserJob = &userJob{}

func (a *userJob) init() func() {
	a.iUserJob = postgres.UserJob
	return func() {}
}

func (a *userJob) onEvent(*event.Data) {}

func (a *userJob) Create(ctx context.Context, in *model.UserJobCreateRequest) error {
	var (
		err error
	)

	c := buildUserJob(in)
	_, err = a.iUserJob.Create(c)
	return err
}

func (a *userJob) List(ctx context.Context, in *model.UserJobListRequest) (*model.UserJobListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.UserJob
		out   = &model.UserJobListResponse{}
	)

	if total, list, err = a.iUserJob.List(in); err != nil {
		return nil, err
	}

	out.Total = total
	out.Data = model.UserJobsEntityToDto(list)

	return out, nil
}

func buildUserJob(in *model.UserJobCreateRequest) *entity.UserJob {
	return &entity.UserJob{

		UserId: in.UserId,

		JobId: in.JobId,
	}
}
